package blackmamba.threads.runnable_enemy;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec;



public class RunnableEnemyTest extends JPanel implements Runnable{
	
	private Thread myThread;
	private String myName;
	private long myDelay;
	
	private int yStart;
	private int xStart;
	
	private int phi;
	private int theta;
	
	private double deltaY;
	private double deltaX;
	private int yWhereDraw;
	private int xWhereDraw;
	private int myModulo = 100;
	
	private double speed;
	
	private int width;
	private int height;
	private JPanel enemyArea;
	
	private final double TO_RAD_CONSTANT = Math.PI / 180 ;
	
	public RunnableEnemyTest(String myName, int width, int startModulo) {
		super();
		this.theta = 0;
		this.speed = 1.0;
		this.width = width;
		this.height = width*3;
		this.deltaY = 0;
		this.deltaX = 0;
		this.myModulo = startModulo;
		this.myName = myName;
		this.myThread = Thread.currentThread();
		init();
		
	}
	
	public void setDelay( long myDelay){
		this.myDelay = myDelay;
		update();
	}
	
	public void setModulo(int myModulo){
		this.myModulo = myModulo;
		update();
	}
	
	public void setSpeed(int speed) {
		this.speed = 0.2*speed;
		update();
	}
	
	public void setPhi( int phi ){
		this.phi = phi;
	}
	
	public void setTheta(int theta) {
		this.theta = theta;
		update();
	}
	
	
	
	
	@Override
	public void run() {
		
		yStart = (height-width)/2;
		xStart = (height-width)/2;
		
		while(true){
			update();
			try {
				myThread.sleep(myDelay);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void update(){
		
		double ShiftModulo = myModulo*Math.sin( System.currentTimeMillis() * TO_RAD_CONSTANT *speed );
		
		deltaY = ShiftModulo * Math.sin( ( theta+ phi) * TO_RAD_CONSTANT);
		deltaX = ShiftModulo * Math.cos( ( theta + phi) * TO_RAD_CONSTANT);
		
		
		yWhereDraw = (int)( yStart + deltaY );
		xWhereDraw = (int)( xStart + deltaX );
		repaint();
	}
	
	public void init(){
		setLayout(new BorderLayout() );
		JLabel nameLabel = new JLabel(myName);
		add( nameLabel, BorderLayout.SOUTH );
		
		enemyArea = new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				drawMyBox(g);
			}
			
			public void drawMyBox(Graphics g){
				g.fillRect(xWhereDraw, yWhereDraw  , width, width);
			}
		};
		
		add(enemyArea, BorderLayout.CENTER);
		
		int nameHeight = nameLabel.getHeight();
		enemyArea.setPreferredSize(new Dimension(height,height));
		
	}
	
	
}
